//
//  HelpContentViewController.swift
//  Exchange
//
//  Created by rui on 2018/7/6.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
import WebKit

class HelpContentViewController: BaseViewController {
    
    var model: HelpDetailModel?
    
    lazy var webView: WKWebView = {
        let v = WKWebView(frame: CGRect(x: 16, y: 0, width: self.view.width - 32, height: self.view.height))
        v.scrollView.showsVerticalScrollIndicator = false
        v.scrollView.showsHorizontalScrollIndicator = false
        v.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        v.navigationDelegate = self
        return v
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()

    }
    func initUI() {
        self.automaticallyAdjustsScrollViewInsets = false
        self.title = model?.title
        self.view.addSubview(webView)
        self.webView.loadHTMLString(model?.content ?? "", baseURL: nil)
    }
    
}

extension HelpContentViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        if ThemeManager.default.currentThemeMode() == .day {
            webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.background='#fdfdfd'", completionHandler: nil)
            webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#393d46'", completionHandler: nil)
        }else {
            webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.background='#30353f'", completionHandler: nil)
            webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#e7ebf4'", completionHandler: nil)
        }


        webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '250%'", completionHandler: nil)
    }
}
